package com.wh.minihelp.user.util;

import java.io.IOException;
import java.util.Random;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import com.wh.json.JSONUtil;
import com.wh.minihelp.filter.IFunctionContext;
import com.wh.minihelp.user.dao.UserDao;
import com.wh.minihelp.user.domain.AccountInfo;
import com.wh.minihelp.user.domain.FamilyAccount;
import com.wh.minihelp.user.service.UserService;

/**
 * 实现用户相关操作：注册、登录、绑定
 * 
 * @author washheart@163.com
 */
public final class UserUtil {
	private static final String SK_Account = "account";
	private static final String SK_WX = "wx";

	public static int getUserFamilyType(final IFunctionContext ctx, final FamilyAccount curFa, boolean response) throws ServletException, IOException {
		int t = FamilyAccount.NotInit;
		if (curFa == null) {
			FamilyAccount tmp = UserUtil.getUser(ctx);
			if (tmp == null) {
				if (response) ctx.forwardModule(UserService.Module, UserService.MD_WX);
			} else {
				t = tmp.getType();
			}
		} else {
			t = curFa.getType();
		}
		return t;
	}

	public static boolean isFamilyManager(final IFunctionContext ctx, final FamilyAccount curFa, boolean response) throws ServletException, IOException {
		int t = getUserFamilyType(ctx, curFa, response);
		if (t == FamilyAccount.NotInit) return false;
		if (t == FamilyAccount.Manger || t == FamilyAccount.CREATOR) return true;
		if (response) ctx.json(JSONUtil.toJsonMsg(false, "只有家庭的创建者和管理者才能执行此操作！"));
		return false;
	}

	public static boolean isFamilyCreator(final IFunctionContext ctx, final FamilyAccount curFa, boolean response) throws IOException, ServletException {
		int t = getUserFamilyType(ctx, curFa, response);
		if (t == FamilyAccount.NotInit) return false;
		if (t == FamilyAccount.CREATOR) return true;
		if (response) ctx.json(JSONUtil.toJsonMsg(false, "只有家庭的创建者和管理者才能执行此操作！"));
		return false;
	}

	public static boolean isFamilyMember(final IFunctionContext ctx, final FamilyAccount curFa, boolean response) throws IOException, ServletException {
		int t = getUserFamilyType(ctx, curFa, response);
		if (t == FamilyAccount.NotInit) return false;
		if (t == FamilyAccount.Manger || t == FamilyAccount.CREATOR || t == FamilyAccount.ByStander) return true;
		if (response) ctx.json(JSONUtil.toJsonMsg(false, "只有被批准加入的家庭成员才能执行此操作！"));
		return false;
	}

	// public static boolean checkUserPerm(IFunctionContext ctx, FamilyAccount fac, String perm) throws ServletException, IOException {
	// if (fac == null) {
	// ctx.forwardModule(UserService.Module, UserService.MD_WX);
	// return false;
	// }
	// return true;
	// }

	public static FamilyAccount getUser(HttpServletRequest req) throws ServletException, IOException {
		return (FamilyAccount) req.getSession().getAttribute(SK_Account);
	}

	public static FamilyAccount getUser(IFunctionContext ctx) throws ServletException, IOException {
		return ctx == null ? null : (FamilyAccount) ctx.getSessionAttribute(SK_Account);
	}

	public static void setUser(IFunctionContext ctx, FamilyAccount fac) {
		ctx.setSessionAttribute(SK_Account, fac);
	}

	public static String genUID() {
		String s = UUID.randomUUID().toString();
		// 去掉“-”符号
		return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
	}

	// // 检测是否已经绑定了微信，如果已经绑定，将帐号信息放到ctx中
	// public static AccountInfo loginByWeixin(IFunctionContext ctx, String wxId) {
	// // FIXME:微信每发一条消息会有一个session吗？
	// AccountInfo account = ctx.getDao(UserDao.class).getAccountByWX(wxId);
	// if (account == null) return null;
	// // UserUtil.setUser(ctx, account);
	// ctx.setSessionAttribute(SK_Account, account);
	// return account;
	// }

	public static String getWxId(IFunctionContext ctx) throws ServletException, IOException {
		return (String) ctx.getSessionAttribute(SK_WX);
	}

	public static void setWxId(IFunctionContext ctx, String wxId) {
		ctx.setSessionAttribute(SK_WX, wxId);
	}

	public static String genPwd(int passwordLength) {
		StringBuffer sb = new StringBuffer();
		int tmp = 0;
		for (int i = 0; i < passwordLength; i++) {
			sb.append(tmp = genPwdNext(tmp));
		}
		return sb.toString();
	}

	private static Random rd = new Random();

	private static int genPwdNext(int cur) {
		switch (cur) {
		case 1:
			return rd.nextBoolean() ? 2 : (rd.nextBoolean() ? 5 : 4);
		case 2:
			return rd.nextBoolean() ? (rd.nextBoolean() ? 1 : 3) : (rd.nextBoolean() ? 4 : (rd.nextBoolean() ? 5 : 6));
		case 3:
			return rd.nextBoolean() ? 2 : (rd.nextBoolean() ? 5 : 6);
		case 4:
			return rd.nextBoolean() ? (rd.nextBoolean() ? 1 : 2) : (rd.nextBoolean() ? 5 : (rd.nextBoolean() ? 7 : 8));
		case 5:
			return rd.nextBoolean() ? (rd.nextBoolean() ? (rd.nextBoolean() ? 1 : 2) : (rd.nextBoolean() ? 3 : 4)) : (rd.nextBoolean() ? (rd.nextBoolean() ? 6 : 7) : (rd.nextBoolean() ? 8 : 9));
		case 6:
			return rd.nextBoolean() ? (rd.nextBoolean() ? 3 : (rd.nextBoolean() ? 3 : 5)) : (rd.nextBoolean() ? 8 : 9);
		case 7:
			return rd.nextBoolean() ? (rd.nextBoolean() ? 4 : 5) : 8;
		case 8:
			return rd.nextBoolean() ? (rd.nextBoolean() ? 4 : 5) : (rd.nextBoolean() ? (rd.nextBoolean() ? 6 : 7) : 9);
		case 9:
			return rd.nextBoolean() ? (rd.nextBoolean() ? 5 : 6) : 8;
		}
		return rd.nextInt(9) + 1;
	}

//	public static void main(String[] args) {
//		for (int i = 0; i < 10; i++) {
//			System.out.println(i + "\t" + genPwd(4));
//		}
//	}
}